package javaer.starter.core.render;

import freemarker.template.TemplateException;
import javaer.starter.base.domain.dto.TemplateDTO;
import javaer.starter.core.handler.ModuleContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 模板渲染
 *
 * @author yangzhen
 * @date 2023/6/12 18:37
 * @since 1.0.0
 */
@Component
public class TemplateRenderHolder {

    @Resource
    private List<INamedFileRender> namedFileRenders;

    @Resource
    private List<ISuffixFileRender> suffixFileRenders;

    @Resource
    private DefaultRender defaultRender;

    public void render(ModuleContext moduleContext, TemplateDTO template) throws TemplateException, IOException {
        boolean isDeal = false;

//        for (INamedFileRender namedFileResolver : namedFileRenders) {
//            if (isDeal = namedFileResolver.isMatch(template.getTemplateName())) {
//                namedFileResolver.doRender(moduleContext, template);
//                break;
//            }
//        }

        if (!isDeal) {
            for (ISuffixFileRender suffixFileResolver : suffixFileRenders) {
                if (isDeal = suffixFileResolver.isMatch(template.getTemplateName())) {
                    suffixFileResolver.doRender(moduleContext, template);
                    break;
                }
            }
        }

        if (!isDeal) {
            defaultRender.doRender(moduleContext, template);
        }
    }
}
